# # !/bin/bash

# GitLab API信息
GITLAB_URL="https://git.realthon.com/api/v4"    # 替换为你的GitLab实例URL
TOKEN="glpat-wxyZPwNvPTvfrdfxxA4d"              # 替换为你的个人访问令牌

# 定义群组及其仓库
declare -A groups
groups=(
    ["ParentGroup1"]="Repo1 Repo2"
    ["ParentGroup2/SubGroup1"]="Repo3 Repo4"
    ["ParentGroup2/SubGroup2"]="Repo5"
)

# 创建群组及仓库
for group in "${!groups[@]}"; do
    # 创建群组
    response=$(curl --request POST "$GITLAB_URL/groups" \
        --header "PRIVATE-TOKEN: $TOKEN" \
        --form "name=${group##*/}" \    # 获取最后一级名称
        --form "path=${group//\//-}" \  # 将路径中的斜杠替换为连字符
        --form "visibility=private")    # 可选: 修改为public或internal

    # 获取新创建的group ID
    group_id=$(echo "$response" | jq -r '.id')

    # 检查group是否创建成功
    if [[ "$group_id" != "null" ]]; then
        echo "Group '$group' created with ID: $group_id"

        # 创建该group下的仓库
        for repo in ${groups[$group]}; do
            repo_response=$(curl --request POST "$GITLAB_URL/projects" \
                --header "PRIVATE-TOKEN: $TOKEN" \
                --form "namespace_id=$group_id" \
                --form "name=$repo" \
                --form "visibility=private")  # 可选: 修改为public或internal

            echo "Repository '$repo' created in group '$group'."
        done
    else
        echo "Failed to create group '$group': $response"
    fi
done






